/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.caelum.interceptor;

import br.com.caelum.controller.ProdutoController;
import br.com.caelum.controller.UsuarioController;
import br.com.caelum.dao.EntidadeDao;
import br.com.caelum.dao.PermissaoDao;
import br.com.caelum.dao.RecursoDao;
import br.com.caelum.model.Entidade;
import br.com.caelum.model.Permissao;
import br.com.caelum.model.Recurso;
import br.com.caelum.model.Usuario;
import br.com.caelum.model.UsuarioWeb;
import br.com.caelum.vraptor.InterceptionException;
import br.com.caelum.vraptor.Intercepts;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.core.InterceptorStack;
import br.com.caelum.vraptor.interceptor.Interceptor;
import br.com.caelum.vraptor.ioc.SessionScoped;
import br.com.caelum.vraptor.resource.ResourceMethod;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.List;

/**
 *
 * @author estagiario
 */
@Intercepts
@SessionScoped
public class AutenticacaoInterceptor implements Interceptor {

    private UsuarioWeb usuarioWeb;
    private Result result;

    public AutenticacaoInterceptor(UsuarioWeb usuarioWeb, Result result) {
        this.usuarioWeb = usuarioWeb;
        this.result = result;
    }

    public void intercept(InterceptorStack is, ResourceMethod rm, Object o) throws InterceptionException {
        result.redirectTo(UsuarioController.class).loginForm();        
    }

    public boolean accepts(ResourceMethod rm) {
        return !this.usuarioWeb.isLogado() && rm.containsAnnotation(Restrito.class);
    }

    @Retention(RetentionPolicy.RUNTIME) //a anotação vai ficar disponível em tempo de execucao
    @Target(ElementType.METHOD) // anotação para métodos
    public @interface Restrito {
        
    }
}